PATH_TARGET = /usr

PATH_SOURCE = ./src
PATH_DATA   = ./data
PATH_TEST   = ./test

CC = gcc
RM = rm
INSTALL = install
GRESOURCE_COMPILER = glib-compile-resources
UPDATE_ICON_CACHE  = gtk-update-icon-cache
UPDATE_DESKTOP     = update-desktop-database

CC_FLAGS   = -Wall -mfpmath=sse -msse -msse2 -pthread
CC_LIBS    = -ladwaita-1 -lgtk-4 -lharfbuzz -lpangocairo-1.0 -lpango-1.0 -lgdk_pixbuf-2.0 \
             -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lglib-2.0 \
	     -lgobject-2.0 -lobject_v2
CC_HEADERS = -I/usr/include/libadwaita-1 -I/usr/include/gtk-4.0 -I/usr/include/pango-1.0 \
 	     -I/usr/include/harfbuzz -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo \
	     -I/usr/include/freetype2 -I/usr/include/graphene-1.0 -I/usr/lib/graphene-1.0/include \
	     -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libmount \
	     -I/usr/include/blkid -I/usr/include/sysprof-6 -I/usr/include/libpng16 \
	     -I/usr/include/pixman-1 -I/usr/include/fribidi -I/usr/include/appstream \
	     -I$(PATH_SOURCE)
RM_FLAGS   = -r -f -v
INSTALL_FLAGS     = -v -c
INSTALL_BIN_FLAGS = -v -m755

DESKTOP_ENTRY = $(PATH_DATA)/EventAndTask.desktop
PROGRAM_ICON  = $(PATH_DATA)/EventAndTask.png
UI_SOURCE     = $(wildcard $(PATH_DATA)/*.ui)
CSS_SOURCE    = $(PATH_DATA)/style.css
GRESOURCE     = $(wildcard $(PATH_DATA)/*.gresource.xml)
SOURCE        = $(wildcard $(PATH_SOURCE)/*.c)
TEST_SOURCE   = $(wildcard $(PATH_TEST)/*.c)

GRESOURCE_SOURCE = $(patsubst %.gresource.xml, %.c, $(GRESOURCE))
GRESOURCE_OBJECT = $(patsubst %.c, %.o, $(GRESOURCE_SOURCE))
OBJECTS     = $(patsubst %.c, %.o, $(SOURCE))
TEST_OBJECT = $(patsubst %.c, %.o, $(TEST_SOURCE))

TEST_TARGET = a.out
TARGET  = kevent

all: $(TARGET)

run: $(TARGET)
	./$(TARGET)

test: $(TEST_TARGET)
	./$(TEST_TARGET)

install: $(TARGET) $(PROGRAM_ICON) $(DESKTOP_ENTRY)
	$(INSTALL) $(INSTALL_FLAGS) $(PROGRAM_ICON)  $(PATH_TARGET)/share/icons/hicolor/512x512/apps/
	$(INSTALL) $(INSTALL_FLAGS) $(DESKTOP_ENTRY) $(PATH_TARGET)/share/applications/
	$(INSTALL) $(INSTALL_BIN_FLAGS) $(TARGET)    $(PATH_TARGET)/bin/
	$(UPDATE_ICON_CACHE) $(PATH_TARGET)/share/icons/hicolor/
	$(UPDATE_DESKTOP)

$(TEST_TARGET): $(TEST_OBJECT) $(GRESOURCE_OBJECT)
	$(CC) $^ $(CC_LIBS) -o $@

$(TARGET): $(OBJECTS) $(GRESOURCE_OBJECT)
	$(CC) $^ $(CC_LIBS) -o $@

$(OBJECTS): %.o : %.c
	$(CC) $< $(CC_HEADERS) $(CC_FLAGS) -c -o $@

$(TEST_OBJECT): %.o : %.c
	$(CC) $< $(CC_HEADERS) $(CC_FLAGS) -c -o $@

$(GRESOURCE_OBJECT): %.o : %.c
	$(CC) $< $(CC_HEADERS) $(CC_FLAGS) -c -o $@

$(GRESOURCE_SOURCE): $(GRESOURCE) $(UI_SOURCE) $(CSS_SOURCE)
	$(GRESOURCE_COMPILER) --sourcedir=$(PATH_DATA) --generate-source $(GRESOURCE) --target=$@

.PHONY: clean
clean:
	$(RM) $(RM_FLAGS) $(TARGET) $(OBJECTS)  \
	$(GRESOURCE_OBJECT) $(GRESOURCE_SOURCE) \
	$(TEST_TARGET) $(TEST_OBJECT)